﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChessOnline
{
    class GameBoard
    {
        GameCell[,] gCells = new GameCell[8,8]; // board 8x8 (rows, columns)
        //Dictionary<Coordinate, GameCell> cells = new Dictionary<Coordinate, GameCell>(64);

        public GameBoard()
        {// При создании инициализируем каждую клетку соответствующими значениями
            // Инициализируаем цветами и координатами
            for (int i = 0; i < 8; i += 1)
            {
                for (int j = 0; j < 8; j += 1)
                {
                    gCells[i, j] = new GameCell();
                    gCells[i, j].Color = (((i+j) % 2 == 0) ? CellColor.White : CellColor.Black);
                    gCells[i, j].Coord = new Coordinate();
                    gCells[i, j].Coord.Number = 8 - i;
                    gCells[i, j].Coord.Literal = (LiteralCoord) j;
                }
            }

            // Расставляем пешки
            for (byte j = 0; j < 8; j += 1)
            {
                // Черные пешки
                gCells[1, j].FigureType = ChessType.BlackPawn;

                // Белые пешки
                gCells[6, j].FigureType = ChessType.WhitePawn;
            }
            
            // Расставляем остальные фигуры
            // i = 0 - черные, i = 7 - белые
            gCells[0, 0].FigureType = ChessType.BlackRook;
            gCells[0, 1].FigureType = ChessType.BlackKnight;
            gCells[0, 2].FigureType = ChessType.BlackBishop;
            gCells[0, 3].FigureType = ChessType.BlackQueen;
            gCells[0, 4].FigureType = ChessType.BlackKing;
            gCells[0, 5].FigureType = ChessType.BlackBishop;
            gCells[0, 6].FigureType = ChessType.BlackKnight;
            gCells[0, 7].FigureType = ChessType.BlackRook;

            gCells[7, 0].FigureType = ChessType.WhiteRook;
            gCells[7, 1].FigureType = ChessType.WhiteKnight;
            gCells[7, 2].FigureType = ChessType.WhiteBishop;
            gCells[7, 3].FigureType = ChessType.WhiteQueen;
            gCells[7, 4].FigureType = ChessType.WhiteKing;
            gCells[7, 5].FigureType = ChessType.WhiteBishop;
            gCells[7, 6].FigureType = ChessType.WhiteKnight;
            gCells[7, 7].FigureType = ChessType.WhiteRook;            
        }

        public List<GameCell> GetCells()
        {
            List<GameCell> list = new List<GameCell>();
            for (int i = 0; i < 8; i += 1)
            {
                for (int j = 0; j < 8; j += 1)
                {
                    list.Add(gCells[i, j]);
                }
            }
            return list;
        }

        public Dictionary<Coordinate, GameCell> GetDicCells()
        {
            Dictionary<Coordinate, GameCell> dic = new Dictionary<Coordinate, GameCell>(64);
            for (int i = 0; i < 8; i += 1)
            {
                for (int j = 0; j < 8; j += 1)
                {
                    Coordinate keyDic = new Coordinate();
                    keyDic = gCells[i, j].Coord;
                    dic.Add(keyDic, gCells[i, j]);
                }
            }
            return dic;
        }

        public void SetCellsFromDictionary(Dictionary<Coordinate, GameCell> cells)
        {
            foreach (var cell in cells)
            {
                gCells[8 - cell.Key.Number, (int)cell.Key.Literal] = cell.Value;
            }
        }


    }
}
